package com.payne;

import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.processor.PageProcessor;

import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;
import java.util.UUID;

public class PictrueSpider implements PageProcessor {

    private Site site=Site.me().setRetryTimes(3).setSleepTime(100)
            .addCookie("Hm_lpvt_d9f1c8630a7aa5c7ce2a72d4b564c044","1555597517")
            .addCookie("Hm_lvt_d9f1c8630a7aa5c7ce2a72d4b564c044","1555597517")
            .addCookie("__51cke__","")
            .addCookie("__51laig__","1")
            .addCookie("__tins__4590702","%7B%22sid%22%3A%201555597517348%2C%20%22vd%22%3A%201%2C%20%22expires%22%3A%201555599317348%7D")
            .setUserAgent("Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0;")
            ;

    private static String url="http://www.jj20.com/";

    public void process(Page page) {
        //System.out.println(page.getHtml().get());

        if(url.equals(page.getUrl().toString())){
            List<String> links = page.getHtml().xpath("//*[@class=\"pic2 fix\"]/li/a/img/@src").all();

            for(int i = 0; i < links.size(); i++){
                String link = links.get(i);
                if(link.startsWith("h")){
                    try {
                        URL url = new URL(link);
                        URLConnection con = url.openConnection();
                        InputStream inStream = con.getInputStream();
                        ByteArrayOutputStream outStream = new ByteArrayOutputStream();
                        byte[] buf = new byte[1024];
                        int len = 0;
                        while((len = inStream.read(buf)) != -1){
                            outStream.write(buf,0,len);
                        }
                        inStream.close();
                        outStream.close();
                        String uuid = UUID.randomUUID().toString();
                        File file = new File("f://test//"+uuid+".jpg");	//图片下载地址
                        FileOutputStream op = new FileOutputStream(file);
                        op.write(outStream.toByteArray());
                        op.close();
                    } catch (MalformedURLException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }

            }
        }

    }

    public Site getSite() {
        return site;
    }

    public static void main(String[] args) {
        Spider.create(new PictrueSpider()).addUrl(url).thread(5).run();
    }


}
